/**
 */
package com.dingpeikeji.quickdeer.core.modules.cupons.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.service.CrudService;
import com.dingpeikeji.quickdeer.core.modules.cupons.dao.TCuponsDao;
import com.dingpeikeji.quickdeer.core.modules.cupons.entity.CuponsInfo;

/**
 * 优惠券Service
 * 
 */
@Service
@Transactional(readOnly = true)
public class TCuponsService extends CrudService<TCuponsDao, CuponsInfo> {

	@Autowired
	private TCuponsDao tCuponsDao;

	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	//获取优惠券领取人信息
	public List<CuponsInfo> getPickInfo(String cuponsId){
		return tCuponsDao.getPickInfo(cuponsId);
	}
	
	//获取已使用优惠券人信息
		public List<CuponsInfo> getUseInfo(String cuponsId){
			return tCuponsDao.getUseInfo(cuponsId);
		}
		
	//更新优惠券信息包括使失效
	public Integer updateCupons(CuponsInfo cuponsInfo){
		return tCuponsDao.updateCupons(cuponsInfo);
	}
		
	//新建优惠券
	public Integer insertCupons(CuponsInfo cuponsInfo){
		return tCuponsDao.insertCupons(cuponsInfo);
	}
	
	//我的优惠券
	public List<CuponsInfo> getMyCupons(String userId){
		List<CuponsInfo> info = tCuponsDao.getMyCupons(userId);
		Date now =new Date();
		for(int i=0;i<info.size();i++){
			CuponsInfo t =info.get(i);
			//固定时间
			if(t.getDateType().equals("0")){
				if(t.getTimeBegin().getTime()> now.getTime()){
					info.get(i).setIsDisable("0");
				}else if(t.getTimeOver().getTime()<now.getTime()){
					info.remove(i);
					i--;
				}
			}
			//当天生效
			if(t.getDateType().equals("1")){
				Calendar c =Calendar.getInstance();
				c.add(Calendar.DAY_OF_MONTH, Integer.parseInt(t.getDateLimit())*-1);//当前时间几天前的时间
				if(t.getPickTime().getTime()>c.getTimeInMillis()){
					info.remove(i);
					i--;
				}
			}
			//次日生效
			if(t.getDateType().equals("2")){
				Calendar c =Calendar.getInstance();
				c.add(Calendar.DAY_OF_MONTH, (Integer.parseInt(t.getDateLimit())+1)*-1);
				if(t.getPickTime().getDate()==now.getDate()){
					info.get(i).setIsDisable("0");
				}else if(t.getPickTime().getTime()>c.getTimeInMillis()){
					info.remove(i);
					i--;
				}
			}
		}
		return info;
	}
	
	//获得注册优惠券
	public Integer registCupons(String userId){
		String id="e3c7b1b6a6044d0994d3aeb0e3d55088";
		CuponsInfo t=tCuponsDao.getCuponsById(id);
		if(Integer.parseInt(t.getStock())>=1){
			CuponsInfo info =new CuponsInfo();
			info.setCuponRecordId(UUID.randomUUID().toString().replace("-", "").toUpperCase());
			info.setUserId(userId);
			info.setWay("注册");
			info.setPickTime(new Date());
			info.setIsUse("0");
			info.setCreateTime(new Date());
			info.setCuponsId(id);
			int i =tCuponsDao.registCupons(info);
			if(i>0){
				int j=tCuponsDao.updateStock(id, (Integer.parseInt(t.getStock())-1)+"");
				if(j>0){
					return j;
				}else{
					return -1;//库存修改失败
				}
			}else{
				return 0;//领取失败
			}
			
		}
		return -2;//优惠券没有库存
	}
	
	//我的可使用优惠券
	public List<CuponsInfo> CanUseCupons(String nowPrice,String userId){
		List<CuponsInfo> info =getMyCupons(userId);
		for(int i=0;i<info.size();i++){
			if(info.get(i).getIsDisable().equals("0")){
				info.remove(i);
				i--;
			}else if(Double.parseDouble(info.get(i).getMinPrice())>Double.parseDouble(nowPrice)){
				info.remove(i);
				i--;
			}
		}
		return info;
	}
	
	//根据id获取注册优惠券信息
	public CuponsInfo getCuponsById(String id){
		CuponsInfo info = tCuponsDao.getCuponsById(id);
		return info;
	}
	
	//使用优惠券更新状态
	public Integer useCupon(CuponsInfo cuponsInfo){
		return tCuponsDao.useCupon(cuponsInfo);
	}
	
	// 根据orderId获得优惠券记录信息
	public CuponsInfo getRecordByOrderId(String orderId){
		return tCuponsDao.getRecordByOrderId(orderId);
	}
	// 根据recordId获得优惠券记录信息
	public CuponsInfo getCuponsByRecordId(String recordId){
		return tCuponsDao.getCuponsByRecordId(recordId);
	}
	
	//外部调用使用优惠券后更新处理
	public Integer dealWithCupon(CuponsInfo cuponsInfo,String orderId,String sid){
		if(orderId==null&&sid.length()>0){
			cuponsInfo.setSid(sid);
		}
		cuponsInfo.setOrderId(orderId);
		cuponsInfo.setUseTime(new Date());
		return useCupon(cuponsInfo);
	}
	
	
	//获得AA拼车优惠券
		public Integer aaCarCupons(String userId){
			String id="4EA0791D3EA945C190FACE91376059F2";
			CuponsInfo t=tCuponsDao.getCuponsById(id);
			if(Integer.parseInt(t.getStock())>=1){
				CuponsInfo info =new CuponsInfo();
				info.setCuponRecordId(UUID.randomUUID().toString().replace("-", "").toUpperCase());
				info.setUserId(userId);
				info.setWay("AA拼车");
				info.setPickTime(new Date());
				info.setIsUse("0");
				info.setCreateTime(new Date());
				info.setCuponsId(id);
				int i =tCuponsDao.registCupons(info);
				if(i>0){
					int j=tCuponsDao.updateStock(id, (Integer.parseInt(t.getStock())-1)+"");
					if(j>0){
						return j;
					}else{
						return -1;//库存修改失败
					}
				}else{
					return 0;//领取失败
				}
				
			}
			return -2;//优惠券没有库存
		}
		
		//AA拼车-sid获取优惠券信息
		public CuponsInfo getRecordBySid(String sid){
			return tCuponsDao.getRecordBySid(sid);
		}
		
		//获得9折优惠券
		public Integer nineCupons(String userId){
			String id="B1870DFCFEB74298A0833B9C6A69EDF5";
			CuponsInfo t=tCuponsDao.getCuponsById(id);
			if(Integer.parseInt(t.getStock())>=1){
				CuponsInfo info =new CuponsInfo();
				info.setCuponRecordId(UUID.randomUUID().toString().replace("-", "").toUpperCase());
				info.setUserId(userId);
				info.setWay("9折优惠券");
				info.setPickTime(new Date());
				info.setIsUse("0");
				info.setCreateTime(new Date());
				info.setCuponsId(id);
				int i =tCuponsDao.registCupons(info);
				if(i>0){
					int j=tCuponsDao.updateStock(id, (Integer.parseInt(t.getStock())-1)+"");
					if(j>0){
						return j;
					}else{
						return -1;//库存修改失败
					}
				}else{
					return 0;//领取失败
				}
				
			}
			return -2;//优惠券没有库存
		}

}
